package cn.school.basics.service.impl;

import cn.school.basics.dao.BaseSubjectDao;
import cn.school.basics.dao.MajorSubjectDao;
import cn.school.basics.entity.MajorSubject;
import cn.school.basics.service.BaseSubjectService;
import cn.school.basics.vo.BaseSubjectVO;
import cn.school.common.utils.SysUserUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BaseSubjectServiceImpl extends ServiceImpl<BaseSubjectDao, BaseSubjectVO> implements BaseSubjectService {

    @Autowired
    MajorSubjectDao majorSubjectDao;

    @Override
    public IPage<BaseSubjectVO> findSubjectPageList(BaseSubjectVO vo) {
        LambdaQueryWrapper<BaseSubjectVO> wrapper = new LambdaQueryWrapper<>();
        //添加租户条件
        wrapper.eq(BaseSubjectVO::getTenantId,vo.getTenantId());
        if(!StringUtils.isEmpty(vo.getSubjectName())){
            wrapper.like(BaseSubjectVO::getSubjectName,vo.getSubjectName());
        }
        if(!StringUtils.isEmpty(vo.getSubjectCode())){
            wrapper.like(BaseSubjectVO::getSubjectCode,vo.getSubjectCode());
        }
        if(!StringUtils.isEmpty(vo.getStatus())){
            wrapper.eq(BaseSubjectVO::getStatus,vo.getStatus());
        }
        if(!StringUtils.isEmpty(vo.getIsRecruit())){
            wrapper.eq(BaseSubjectVO::getIsRecruit,vo.getIsRecruit());
        }
        wrapper.orderByDesc(BaseSubjectVO::getCreateTime);
        IPage<BaseSubjectVO> page = page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
        return page;

    }

    @Override
    public int addOrUpdateSubject(BaseSubjectVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        LambdaQueryWrapper<BaseSubjectVO> wrapper = new LambdaQueryWrapper<>();
        int num=0;
        if(StringUtils.isEmpty(vo.getId())){
            wrapper.eq(BaseSubjectVO::getSubjectCode,vo.getSubjectCode())
                    .eq(BaseSubjectVO::getTenantId,SysUserUtils.getSysUser().getTenantId());
            int count = this.baseMapper.selectCount(wrapper);
            if(count>0){
                return -1;
            }
            vo.setCreateBy(username);
            vo.setCreateTime(new Date());
            num = this.baseMapper.insert(vo);

            //插入专业科目关联数据
            List<Long> subjectIds = vo.getSubjectIds();
            List<MajorSubject> majorSubList = new ArrayList();
            if(subjectIds != null && subjectIds.size() > 0){
                for (int i = 0; i < subjectIds.size(); i++) {
                    MajorSubject majorSubject = new MajorSubject();
                    majorSubject.setMajorId(vo.getId());
                    majorSubject.setSubjectId(subjectIds.get(i));
                    majorSubList.add(majorSubject);
                }
            }
            majorSubjectDao.insertBatch(majorSubList);
        }else{
            wrapper.eq(BaseSubjectVO::getSubjectCode,vo.getSubjectCode());
            wrapper.ne(BaseSubjectVO::getId,vo.getId());
            int count = this.baseMapper.selectCount(wrapper);
            if(count>0){
                return -1;
            }
            vo.setModifyBy(username);
            vo.setModifyTime(new Date());
            num = this.baseMapper.updateById(vo);

            //删除以前专业-科目关联关系
            LambdaQueryWrapper<MajorSubject> msWrapper = new LambdaQueryWrapper<>();
            msWrapper.eq(MajorSubject::getMajorId,vo.getId());
            majorSubjectDao.delete(msWrapper);

            //插入专业科目关联数据
            List<Long> subjectIds = vo.getSubjectIds();
            List<MajorSubject> majorSubList = new ArrayList();
            if(subjectIds != null && subjectIds.size() > 0){
                for (int i = 0; i < subjectIds.size(); i++) {
                    MajorSubject majorSubject = new MajorSubject();
                    majorSubject.setMajorId(vo.getId());
                    majorSubject.setSubjectId(subjectIds.get(i));
                    majorSubList.add(majorSubject);
                }
            }
            majorSubjectDao.insertBatch(majorSubList);
        }
        return num;
    }

    @Override
    public int deleteSubjectById(List<Long> ids) {
        int num=0;
        for(Long id:ids){
            this.baseMapper.deleteById(id);
            num++;
        }
        return num;
    }

    @Override
    public List<MajorSubject> getMajorAllSubj(Long majorId) {
        LambdaQueryWrapper<MajorSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MajorSubject::getMajorId,majorId);
        return majorSubjectDao.selectList(wrapper);
    }

    @Override
    public List<MajorSubject> getClassAllSubj(String classId) {
        return majorSubjectDao.getClassAllSubj(classId);
    }
}
